import logging
logger = logging.getLogger('utptheme.ocf.patches')

def apply_patches():
    pass
#    _apply_restrictedTraverse_patch()


def _apply_restrictedTraverse_patch():
    try:
       import OFS.Traversable
    except:
       return

    patched = getattr(OFS.Traversable,'_inigo_restrictedTraverse_patched',False)
    if patched:
       return

    logger.info("MonkeyPatching restrictedTraverse")

    from zope.component import queryMultiAdapter

    OFS.Traversable.Traversable.__inigo_orig_restrictedTraverse = OFS.Traversable.Traversable.restrictedTraverse

    def restrictedTraverse(self, path, *args, **kwargs):
        request = getattr(self,'REQUEST',None)
        if request:
           view = queryMultiAdapter((self, self.REQUEST),name=path)
           if view is not None:
              if hasattr(view,'__of__'):
                 return view.__of__(self)

        return self.__inigo_orig_restrictedTraverse(path,*args,**kwargs)

    OFS.Traversable.Traversable.restrictedTraverse = restrictedTraverse
    OFS.Traversable._inigo_restrictedTraverse_patched = True
